<template>
	<div class="login_box">
		<el-card class="box-card">
			<div slot="header" class="clearfix" style="text-align: center;">
				<span>用户登录</span>
			</div>
			<!-- 请输入用户名 -->
			<el-form :model="adminObj" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
				<el-form-item prop="username">
					<el-input v-model="adminObj.username" autocomplete="off" placeholder="请输入用户名"></el-input>
				</el-form-item>
				<!-- 请输入密码-->
				<el-form-item prop="password">
					<el-input type="password" v-model="adminObj.password" autocomplete="off" placeholder="请输入密码">
					</el-input>
				</el-form-item>
				<!-- 请输入验证码-->
				<el-form-item prop="code">
					<el-input v-model="adminObj.code" autocomplete="off"
					 placeholder="请输入验证码" style="width: 155px;"></el-input>
					<!-- 显示验证码、并调整与字体居中 -->
					<img :src="codeImg" alt="" style="vertical-align: middle;">
				</el-form-item>
				<!-- 确认提交 -->
				<el-form-item style="text-align: center;">
					<el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
					<el-button @click="resetForm('ruleForm')">重置</el-button>
				</el-form-item>
			</el-form>
		</el-card>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				//登录相关
				codeImg: "", //显示验证码的方法
				adminObj: {
					username: "",
					password: "",
					code: ""
				},
				rules: {
					username: [{
						required: true,
						message: '请输入用户名！！',
						trigger: 'blur'
					}],
					password: [{
						required: true,
						message: '请输密码！！',
						trigger: 'blur'
					}],
					code: [{
						required: true,
						message: '请输入验证码！！',
						trigger: 'blur'
					}],
				},
				userObj: ""
			}
		},
		mounted() {
			// this.getCode()
			this.codeImg='/login/captchaCode?date='+new Date();
		},
		methods: {
			// getCode(){
			//      this.codeImg='/login/loginVerifyCode?data='+new Date();
			// 	 console.log("33333333")
			// },
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						/* alert('请输入账号和密码!'); */
						this.postRequest("/login/login_erp",this.adminObj).then(resp => {
							if (resp) {
								this.userObj = resp.data.adminObj;
								// 路由跳转
								this.$router.push("/main");
								sessionStorage.setItem("user", JSON.stringify(this.userObj));
							}
						})
						// this.$router.push('/main');
						/* 登录成功之后 */
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			}
		}
	}
</script>

<style>
	.box-card {
		width: 450px;
		height: 300px;
	}

	.login_box {
		display: flex;
		height: 100vh;
		/* 水平居中 */
		justify-content: center;
		/* 垂直居中 */
		align-items: center;
	}
</style>
